package com.nuc.zyt.day01;

import com.nuc.zyt.day01.Annotation.DIYAAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @ClassName HelloClass
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/5/14 12:38
 * @Version 1.0
 */

public class HelloClass {
    public static void main(String[] args) throws ClassNotFoundException {
        try{
            // 获取class对象
            Class<?> aClass = Class.forName("com.nuc.zyt.day01.Person");
            System.out.println(aClass);
            // 获取无参构造器
            Object o1 = aClass.newInstance();
            System.out.println("默认无参构造函数对象"+o1);


            // 获取有参构造器
            Constructor<?> constructor = aClass.getConstructor(String.class,int.class);
            Object o = constructor.newInstance("赵元通666",18);

            System.out.println("有参构造函数创建对象"+o);



            System.out.println("_________________分割________________");
            // 利用反射赋值
            System.out.println("给无参构造的对象赋值");
            Field name = aClass.getDeclaredField("name");
            Field age = aClass.getDeclaredField("age");
            // 反射给私有字段赋值需要权限，下面将权限设置为true
            name.setAccessible(true);
            name.set(o1,"林俊杰");
            age.setAccessible(true);
            age.set(o1,999);
            System.out.println(o1);


            System.out.println("-------------------分割--------------------");
            // 利用反射 调用方法  获取本类中所有方法除去父类
            Method[] declaredMethods = aClass.getDeclaredMethods();
            Method getString = aClass.getDeclaredMethod("getString", Integer.class);
            Object invoke1 = getString.invoke(o1, 1);
            System.out.println("方法返回的字符串:"+invoke1);

            // 利用反射 获取方法上的注解信息
            SuppressWarnings annotation = getString.getAnnotation(SuppressWarnings.class);
            System.out.println("方法上的注解"+annotation);


            // 自定义注解的使用
            DIYAAnnotation annotation1 = getString.getAnnotation(DIYAAnnotation.class);
            System.out.println(annotation1);




            /*
            * 创建实例的难点在于，很多人不知道clazz.newInstance()底层还是调用Contructor对象的newInstance()，
            * 所以，要想调用clazz.newInstance()，必须保证编写类的时候有个无参构造，否则就要先获取对应的Constructor。
            * */

            System.out.println("_________________分割________________");
            Class<Person> personClass = Person.class;
            System.out.println(personClass);
            // 利用反射调用方法 前提需要对象
            Person person1 = personClass.newInstance();
            System.out.println("第三个person对象"+person1);

            Method getName = personClass.getMethod("getName");
            Object invoke = getName.invoke(person1);
            System.out.println(invoke);

            // 类加载器
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            System.out.println("系统类加载器"+systemClassLoader);
            ClassLoader parent = systemClassLoader.getParent();
            System.out.println("扩展类加载器"+parent);
            ClassLoader parent1 = parent.getParent();
            System.out.println("c++编写的根加载器 java获取不到"+parent1);

            Class<?> aClass1 = Class.forName("com.nuc.zyt.day01.HelloClass");
            ClassLoader classLoader = aClass1.getClassLoader();
            System.out.println("当前类的类加载器"+classLoader);
            // jdk内置类加载器
            Class<?> aClass2 = Class.forName("java.lang.reflect.Array");
            ClassLoader classLoader1 = aClass2.getClassLoader();
            System.out.println("jdk内置类加载器"+classLoader1);

            //
            System.out.println( System.getProperty("java.class.path"));
            /*
            * D:\jdk1\jdk8\jre\lib\charsets.jar;
            * D:\jdk1\jdk8\jre\lib\deploy.jar;
            * D:\jdk1\jdk8\jre\lib\ext\access-bridge-64.jar;
            * D:\jdk1\jdk8\jre\lib\ext\cldrdata.jar;
            * D:\jdk1\jdk8\jre\lib\ext\dnsns.jar;
            * D:\jdk1\jdk8\jre\lib\ext\jaccess.jar;
            * D:\jdk1\jdk8\jre\lib\ext\jfxrt.jar;
            * D:\jdk1\jdk8\jre\lib\ext\localedata.jar;
            * D:\jdk1\jdk8\jre\lib\ext\nashorn.jar;
            * D:\jdk1\jdk8\jre\lib\ext\sunec.jar;
            * D:\jdk1\jdk8\jre\lib\ext\sunjce_provider.jar;
            * D:\jdk1\jdk8\jre\lib\ext\sunmscapi.jar;
            * D:\jdk1\jdk8\jre\lib\ext\sunpkcs11.jar;
            * D:\jdk1\jdk8\jre\lib\ext\zipfs.jar;
            * D:\jdk1\jdk8\jre\lib\javaws.jar;
            * D:\jdk1\jdk8\jre\lib\jce.jar;
            * D:\jdk1\jdk8\jre\lib\jfr.jar;
            * D:\jdk1\jdk8\jre\lib\jfxswt.jar;
            * D:\jdk1\jdk8\jre\lib\jsse.jar;
            * D:\jdk1\jdk8\jre\lib\management-agent.jar;
            * D:\jdk1\jdk8\jre\lib\plugin.jar;
            * D:\jdk1\jdk8\jre\lib\resources.jar;
            * D:\jdk1\jdk8\jre\lib\rt.jar;
            * E:\gitprojects\project\out\production\Day01;
            * E:\idea\IntelliJ IDEA 2021.3.3\lib\idea_rt.ja

            * */


        }catch (Exception e){

        }finally {

        }


    }
}
@SuppressWarnings("all")
class Person{
    private String name ="赵元通";
    private int age = 19;

    public Person(){}

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name+age;
    }
    @DIYAAnnotation(name="赵元通")
    public String getString(Integer i){
        System.out.println("这里是个方法");
        return "赵元通66666";
    }
}